
from Tkinter import *
import ctypes

SET_SERVO = 1
KILL_CREEPA = 2
KILL_CREEPB = 3
SET_DUTY = 4
global servospot
servospot=0

print "set servospot"
opponentpin = []
playerpin = []


usb = ctypes.cdll.LoadLibrary('usb.dll')
usb.initialize()
print "hey o"
buffer = ctypes.c_buffer(8)
player=True

dev = usb.open_device(0x6666, 0x0003, 0)

##def set_ra1_callback():
##    usb.control_transfer(dev, 0x40, SET_RA1, 0, 0, 0, buffer)
##
##def clr_ra1_callback():
##    usb.control_transfer(dev, 0x40, CLR_RA1, 0, 0, 0, buffer)

# def get_ra2_callback():
#     usb.control_transfer(dev, 0xC0, GET_RA2, 0, 0, 1, buffer)
#     status.configure(text = 'RA2 is currently %d.' % ord(buffer[0]))

def set_duty_callback(value):
    usb.control_transfer(dev, 0x40, SET_SERVO, int(value), 0, 0, buffer)
    
def set_callback(pin):
    global player
    if not (pin in opponentpin or pin in playerpin):
        usb.control_transfer(dev, 0x40, pin, 0, 0, 0, buffer)
        img=PhotoImage(file=['X','O'][int(player)]+'.gif')
        buttons[pin].configure(image=img)
        buttons[pin].image=img
        if player:
            opponentpin.append(pin)
        else:
            playerpin.append(pin)
        player = not player
    
def make_callback(pin):
    return lambda: set_callback(pin)

def update_status():
    global servospot
    usb.control_transfer(dev, 0xC0, servospot, 0, 0, 1, buffer)
    status.configure(text = 'Servo at %d' % servospot)
    root.after(50, update_status)
    servospot+=1
    if servospot>255:
        servospot=0

def kill_spot_number(kill_spot):
    usb.control_transfer(dev, 0x40, kill_spot, 0, 0, 0, buffer)



        
##def clear_board():
##        usb.control_transfer(dev, 0x40, 9, 0, 0, 0, buffer)
def make_updater(servospot=0):
    def update():
        usb.control_transfer(dev, 0xC0, servospot, 0, 0, 1, buffer)
        status.configure(text = 'Servo at %d' % servospot)
        servospot+=1
        if servospot>255:
            servospot=0
        print servospot
        root.after(1, make_updater(servospot))
    return update
        
root = Tk()
root.title('Kill Tower GUI')
fm = Frame(root)
##b=Button(root, width=20,text="First").grid(row=0)
##b=Button(root, width=20,text="Second").grid(row=1)
##b=Button(root, width=18,text="PAUL BOOTH").grid(row=0, column=1)
##b=Button(root, width=19,text="Blair's hair").grid(row=7, column=8)
buttons=[]
##img=PhotoImage(file='blank.GIF')
##for i in xrange(9):
    
##    print img
##    b=Button(root,  width=64,height=64, text=' ', image=img,command = make_callback(i))
##    b.image=img
##    b.grid(row=i/3, column=i%3)
##    buttons.append(b)


    #b.pack(side = LEFT, padx=2, pady=2)
    

#Button(fm, text = ' ', command = clr_ra1_callback).pack(side = LEFT)
# Button(fm, text = 'GET_RA2', command = get_ra2_callback).pack(side = LEFT)

dutyslider = Scale(root, from_ = 0, to = 255, orient = HORIZONTAL, showvalue = FALSE, command = set_duty_callback)
dutyslider.set(128)
dutyslider.pack(side = TOP)

for i in range(7):
    b = Button(fm, text = 'button', command = kill_spot_number(i)).pack(side = LEFT)
##status = Label(root, text = 'Get Ready for FUN.')
##status.grid(row=3,column=0,columnspan=3)
##b=Button(root, text = 'clear board', command = clear_board);
##b.grid(row=4,column=0,columnspan=3)
#status.pack(side = TOP)
    fm.pack(side = TOP)

dev = usb.open_device(0x6666, 0x0003, 0)
if dev<0:
    print "No matching device found...\n"
else:
    ret = usb.control_transfer(dev, 0x00, 0x09, 1, 0, 0, buffer)
    if ret<0:
        print "Unable to send SET_CONFIGURATION standard request.\n"
    clear_board()
    root.after(30, update_status)
    root.mainloop()
    usb.close_device(dev)
